const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    proList: [],
    more: true,
    moveData1: null,
    moveData2: null,
    inputShowed: false,
    inputVal: "",
    inputVal2: "搜索",
    isHideLoadMore: true,
    page: 1,
    key: '',
    chukupage: false,
    offerpage: false,
    masking: false,
    houseid:null,
    houseList: [],
    image: '../images/screen.png',
  },
  changeText: function (e) {
    let name = e.currentTarget.dataset.name;
    let nameMap = {}
    nameMap[name] = e.detail && e.detail.value
    this.setData(nameMap)
  },
  clickScreen: function () {
    if (this.data.masking) {
      this.setData({
        masking: false,
        image: '../images/screen.png'
      })
    } else {
      this.setData({
        masking: true,
        image: '../images/screen-active.png'
      })
    }
  },
  sltScn: function (e) {
    console.log(e)
    var id = e.currentTarget.dataset.id ? e.currentTarget.dataset.id : null
    console.log(id)
    this.setData({
      page: 1,
      houseid: id
    })
    this.getList()
  },
  search: function () {
    this.setData({
      page: 1
    })
    this.getList()
  },
  open: function (e) {
    var id = e.currentTarget.dataset.id
    var that = this
    
    if(this.data.chukupage || this.data.offerpage){
      var that = this
      wx.showActionSheet({
        itemList: ['选择产品', '查看详情'],
        success: function (res) {
          if (!res.cancel) {
            if (res.tapIndex == 0) {
              let pages = getCurrentPages(); //获取当前页面js里面的pages里的所有信息。
              let prevPage = pages[pages.length - 2];
              var obj = e.currentTarget.dataset.item
              var newObj = {}
              if(that.data.chukupage){
                newObj.ProductId = obj.ProductId
                newObj.ProductNo = obj.ProductNo
                newObj.ProductName = obj.ProductName
                newObj.Unit = obj.Product.Unit
                newObj.HouseName = obj.HouseName
                newObj.Quantity = 1
                newObj.maxNum = obj.Quantity
                var arr = prevPage.data.proList
                console.log(newObj)
                arr.push(newObj)
                prevPage.setData({
                  proList: arr
                })
              } else if(that.data.offerpage){
                newObj.ProductId = obj.ProductId
                newObj.ProductNo = obj.ProductNo
                newObj.ProductName = obj.ProductName
                newObj.Unit = obj.Product.Unit
                newObj.Quantity = 1
                newObj.Price = 1
                newObj.remark = ""
                var arr = prevPage.data.proList
                arr.push(newObj)
                prevPage.setData({
                  proList: arr
                })
              }
              
              wx.navigateBack({
                delta: 1
              })
            } else if (res.tapIndex == 1) {
              wx.navigateTo({
                url: './previewStore/previewStore?id=' + id
              })
            }
          }
        }
      });
    } else {
      wx.navigateTo({
        url: './previewStore/previewStore?id=' + id
      })
    }
  },
  getList: function () {
    var that = this
    app.req({
      url: 'Product/Store',
      data: {
        pageindex: this.data.page,
        pagesize: 15,
        key: this.data.key
      },
      success: function (res) {
        // if (res.data.Result.length > 0) {
          var arr = res.data.result
          for (let i = 0; i < arr.length; i++) {
            arr[i].date = []
            var arr1 = app.dateFormat(arr[i].createTime)
            // arr[i].date.concat(arr1)
            arr[i].date.push(arr1[0])
            arr[i].date.push(arr1[1])
          }
          if (that.data.page == 1) {
            that.setData({
              proList: arr
            })
          } else if (that.data.page > 1) {
            that.setData({
              proList: that.data.proList.concat(arr)
            })
          }
        // } 
        if(res.data.result.length == 0) {
          that.setData({
            page: that.data.page - 1
          })
        }
        setTimeout(function () {
          wx.stopPullDownRefresh()
        }, 1000)

      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    var that = this
    if (options.type == 4) {
      this.setData({
        chukupage: true,
        houseid: options.houseid
      })
    } else if (options.type == 5) {
      this.setData({
        offerpage: true
      })
    }
    // app.req({
    //   url:"Configure/Houses",
    //   success: function(res){
    //     console.log(res.data.Result)
    //     that.setData({
    //       houseList: res.data.Result
    //     })
    //   }
    // })
    this.getList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.startPullDownRefresh({

    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    if (this.data.chukupage) {
      this.setData({
        page: 1,
        key: ''
      })
    } else {
      this.setData({
        page: 1,
        key: '',
        houseid: null
      })
    }
    console.log(this.data.houseid)
    this.getList()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log('加载更多')
    this.setData({
      isHideLoadMore: false,
    })
    this.setData({
      page: this.data.page + 1
    })
    this.getList()
    setTimeout(() => {
      this.setData({
        isHideLoadMore: true,
      })
    }, 1000)

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})